package com.tf.six.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tf.six.entity.Cart;
import com.tf.six.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "购物车接口")
@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    private Integer getCurrentUserId(CouchbaseProperties.Authentication authentication) {
        // 实际项目中从认证信息获取用户ID
        return 1; // 示例用户ID
    }

    @Operation(summary = "获取购物车") // 使用 @Operation 替代 @ApiOperation
    @GetMapping
    public List<Cart> getCart(CouchbaseProperties.Authentication authentication) {
        Integer userId = getCurrentUserId(authentication);
        return cartService.list(new QueryWrapper<Cart>().eq("user_id", userId));
    }



    @Operation(summary = "添加商品到购物车")
    @PostMapping
    public boolean addToCart(@RequestBody Cart cart, CouchbaseProperties.Authentication authentication) {
        cart.setUserId(getCurrentUserId(authentication));
        return cartService.save(cart);
    }


    @Operation(summary = "更新购物车数量")
    @PutMapping("/{id}")
    public boolean updateQuantity(@PathVariable Integer id, @RequestParam Integer quantity) {
        return cartService.updateQuantity(id, quantity);
    }



    @Operation(summary = "删除购物车项")
    @DeleteMapping("/{id}")
    public boolean removeFromCart(@PathVariable Integer id) {
        return cartService.removeById(id);
    }


}
